package com.syezon.wifikey.download;


import com.syezon.android.base.download.DownloadTask;
import com.syezon.android.base.download.IDownloadTaskListener;
import com.syezon.android.base.utils.ApkUtil;
import com.syezon.android.base.utils.LogUtils;
import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.notification.DownloadNotification;
import com.syezon.wifikey.utils.Utils;

/**
 * Created by Administrator on 2018/6/26.
 */

public class UrlDownloadListener implements IDownloadTaskListener {


    private static final String TAG = UrlDownloadListener.class.getSimpleName();

    private String url;


    public UrlDownloadListener(String url) {
        this.url = url;
    }

    @Override
    public void onPrepare(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onPrepare");
    }

    @Override
    public void onStart(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onStart");
    }

    @Override
    public void onDownloading(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onDownloading");
        try {
            DownloadNotification.getDownloadNotification(Utils.getApp()).notifyDownloadProgress(downloadTask, url);
        } catch (Exception e) {

        }
    }

    @Override
    public void onPause(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onPause");
    }

    @Override
    public void onCancel(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onCancel");
    }

    @Override
    public void onCompleted(DownloadTask downloadTask) {
        LogUtils.eTag(TAG, "onCompleted");
        try {
            String path = downloadTask.getSaveDirPath() + downloadTask.getFileName();
            ApkUtil.ApkInfo apkInfo = ApkUtil.getApkInfo(Utils.getApp(), path);
            if (apkInfo != null) {
                ApkUtil.installApk(Utils.getApp(), path);
            } else {
                ToastUtils.showShort("下载完成!");
            }
            DownloadNotification.getDownloadNotification(Utils.getApp()).notifyDownloadFinish(downloadTask, url);
        } catch (Exception e) {
        }
    }

    @Override
    public void onError(DownloadTask downloadTask, int errorCode) {
        LogUtils.eTag(TAG, "onError " + errorCode);
    }
}
